package com.qingke.hibernate.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import com.qingke.hibernate.bean.SchoolYear;
import com.qingke.hibernate.bean.Student;

public class SystemDAO {
	protected Session session;
	public SystemDAO(Session session){
		this.session=session;
	}

	public List<SchoolYear> listSchoolYear(){
		Query<SchoolYear> query=session.createQuery("from SchoolYear", SchoolYear.class);
		return query.getResultList();
	}
//	public List<?> listMSG(){
//		Query<?> query=session.createQuery("select s.sid,s.firstname,s.lastname,ap.building,ap.floor,ap.room "
//				+ "from Student s,Apartment ap,Accommodation  ac "
//				+ "where ac.apartmentId=ap.id and s.accommodationId=ac.id");
//		
//		return query.getResultList();
//	}
	public void insert(Object obj){
		Transaction tx = session.beginTransaction();
		try{
			session.save(obj);
			tx.commit();
			
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void update(Object obj){
		Transaction tx=session.beginTransaction();
		try{
			session.update(obj);
			tx.commit();
			
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
		}
	}
	public void delete(Object obj){
		Transaction tx=session.beginTransaction();
		try{
			session.delete(obj);
			tx.commit();
			
		}catch(HibernateException e){
			e.printStackTrace();
			tx.rollback();
		}
	}
}
